<html lang="zh-CN"><head><meta charset="UTF-8"><style>.nodata  main {width:1000px;margin: auto;}</style></head><body class="nodata " style=""><div class="main_father clearfix d-flex justify-content-center " style="height:100%;"> <div class="container clearfix " id="mainBox"><main><div class="blog-content-box">
<div class="article-header-box">
<div class="article-header">
<div class="article-title-box">
<h1 class="title-article" id="articleContentId">(B卷,100分)- TLV解码（Java & JS & Python & C）</h1>
</div>
</div>
</div>
<div id="blogHuaweiyunAdvert"></div>

        <div id="article_content" class="article_content clearfix">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/kdoc_html_views-1a98987dfd.css">
        <link rel="stylesheet" href="https://csdnimg.cn/release/blogv2/dist/mdeditor/css/editerView/ck_htmledit_views-044f2cf1dc.css">
                <div id="content_views" class="htmledit_views">
                    <h4 id="main-toc">题目描述</h4> 
<p>TLV编码是按[Tag Length Value]格式进行编码的&#xff0c;一段码流中的信元用Tag标识&#xff0c;Tag在码流中唯一不重复&#xff0c;Length表示信元Value的长度&#xff0c;Value表示信元的值。</p> 
<p>码流以某信元的Tag开头&#xff0c;Tag固定占一个字节&#xff0c;Length固定占两个字节&#xff0c;字节序为<strong>小端序</strong>。</p> 
<p>现给定TLV格式编码的码流&#xff0c;以及需要解码的信元Tag&#xff0c;请输出该信元的Value。</p> 
<p>输入码流的16进制字符中&#xff0c;不包括小写字母&#xff0c;且要求输出的16进制字符串中也不要包含小写字母&#xff1b;码流字符串的最大长度不超过50000个字节。</p> 
<p></p> 
<h4 id="%E8%BE%93%E5%85%A5%E6%8F%8F%E8%BF%B0">输入描述</h4> 
<ul><li>输入的第一行为一个字符串&#xff0c;表示待解码信元的Tag&#xff1b;</li><li>输入的第二行为一个字符串&#xff0c;表示待解码的16进制码流&#xff0c;字节之间用空格分隔。</li></ul> 
<p></p> 
<h4 id="%E8%BE%93%E5%87%BA%E6%8F%8F%E8%BF%B0">输出描述</h4> 
<ul><li>输出一个字符串&#xff0c;表示待解码信元以16进制表示的Value。</li></ul> 
<p></p> 
<h4 id="%E7%94%A8%E4%BE%8B">用例</h4> 
<table border="1" cellpadding="1" cellspacing="1" style="width:613px;"><tbody><tr><td style="width:86px;">输入</td><td style="width:523px;">31<br /> 32 01 00 AE 90 02 00 01 02 30 03 00 AB 32 31 31 02 00 32 33 33 01 00 CC</td></tr><tr><td style="width:86px;">输出</td><td style="width:523px;">32 33</td></tr><tr><td style="width:86px;">说明</td><td style="width:523px;"> <p>需要解析的信元的Tag是31&#xff0c;</p> <p>从码流的起始处开始匹配&#xff0c;</p> <p>第一个信元的Tag是32&#xff0c;信元长度为1&#xff08;01 00&#xff0c;小端序表示为1&#xff09;&#xff1b;</p> <p>第二个信元的Tag是90&#xff0c;其长度为2&#xff1b;</p> <p>第三个信元的Tag是30&#xff0c;其长度为3&#xff1b;</p> <p>第四个信元的Tag是31&#xff0c;其长度为2&#xff08;02 00&#xff09;&#xff0c;</p> <p>所以返回长度后面的两个字节即可&#xff0c;即32 33。</p> </td></tr></tbody></table> 
<p></p> 
<h4 id="%E9%A2%98%E7%9B%AE%E8%A7%A3%E6%9E%90">题目解析</h4> 
<p>本题题目可能比较难理解&#xff0c;但是大概意思如下&#xff1a;</p> 
<p>第二行输入的码流&#xff0c;是由多个信元组成的&#xff0c;每个信元又是由tag、len、val组成</p> 
<p>其中tag占一个字节&#xff0c;len占两个字节&#xff0c;而val占的的字节数由len决定&#xff0c;因此上面用例的第二行输入如下图&#xff1a;</p> 
<p><img alt="" height="152" src="https://img-blog.csdnimg.cn/623ae93c6ae649e8acec678bb9a5baf7.png" width="1141" /></p> 
<p>可能这样大家就一目了然了吧。 </p> 
<p>现在要找tag为31信元的val&#xff0c;从上图可以看出val为32 33</p> 
<p></p> 
<h4 id="%E7%AE%97%E6%B3%95%E6%BA%90%E7%A0%81">JavaScript算法源码</h4> 
<pre><code class="language-javascript">/* JavaScript Node ACM模式 控制台输入获取 */
const readline &#61; require(&#34;readline&#34;);

const rl &#61; readline.createInterface({
  input: process.stdin,
  output: process.stdout,
});

const lines &#61; [];
rl.on(&#34;line&#34;, (line) &#61;&gt; {
  lines.push(line);

  if (lines.length &#61;&#61;&#61; 2) {
    // 待解码信元的Tag
    const tag &#61; lines[0];

    // 待解码的16进制码流
    const stream &#61; lines[1].split(&#34; &#34;);

    console.log(getResult(stream, tag));

    lines.length &#61; 0;
  }
});

function getResult(stream, find) {
  // 这里反转数组是为了后面避免shift操作&#xff0c;转而使用pop操作&#xff0c;pop的性能更优一点
  stream.reverse();

  while (stream.length) {
    const tag &#61; stream.pop();

    const len &#61; parseInt(
      // 由于是小端序&#xff0c;因此需要反转
      [stream.pop(), stream.pop(), &#34;0x&#34;].reverse().join(&#34;&#34;),
      16
    );

    const val &#61; [];
    for (let i &#61; 0; i &lt; len; i&#43;&#43;) {
      val.push(stream.pop());
    }

    if (tag &#61;&#61;&#61; find) {
      return val.join(&#34; &#34;);
    }
  }
}
</code></pre> 
<p></p> 
<h4>Java算法源码</h4> 
<pre><code class="language-java">import java.util.ArrayList;
import java.util.Scanner;
import java.util.StringJoiner;

public class Main {
  public static void main(String[] args) {
    Scanner sc &#61; new Scanner(System.in);

    String target &#61; sc.nextLine();
    String[] stream &#61; sc.nextLine().split(&#34; &#34;);

    System.out.println(getResult(stream, target));
  }

  public static String getResult(String[] stream, String target) {
    int i &#61; 0;

    while (i &lt; stream.length) {
      String tag &#61; stream[i&#43;&#43;];

      String tmp1 &#61; stream[i&#43;&#43;];
      String tmp2 &#61; stream[i&#43;&#43;];

      int len &#61; Integer.parseInt(tmp2 &#43; tmp1, 16);

      ArrayList&lt;String&gt; val &#61; new ArrayList&lt;&gt;();
      for (int j &#61; 0; j &lt; len; j&#43;&#43;) {
        val.add(stream[i&#43;&#43;]);
      }

      if (tag.equals(target)) {
        StringJoiner sj &#61; new StringJoiner(&#34; &#34;);
        for (String s : val) sj.add(s);
        return sj.toString();
      }
    }

    return null;
  }
}
</code></pre> 
<p></p> 
<h4>Python算法源码</h4> 
<pre><code class="language-python"># 输入获取
target &#61; input()
stream &#61; input().split()


# 算法入口
def getResult():
    stream.reverse()

    while len(stream) &gt; 0:
        # 这里反转数组是为了后面避免shift操作&#xff0c;转而使用pop操作&#xff0c;pop的性能更优一点
        tag &#61; stream.pop()

        tmp &#61; [stream.pop(), stream.pop(), &#34;0x&#34;]
        #  由于是小端序&#xff0c;因此需要反转
        tmp.reverse()

        long &#61; int(&#34;&#34;.join(tmp), 16)

        val &#61; []
        for i in range(long):
            val.append(stream.pop())

        if tag &#61;&#61; target:
            return &#34; &#34;.join(val)


# 算法调用
print(getResult())
</code></pre> 
<h4>C算法源码</h4> 
<pre><code class="language-cpp">#include &lt;stdio.h&gt;
#include &lt;string.h&gt;

#define MAX_SIZE 100000

int main() {
    char target[4];
    gets(target);

    char stream[MAX_SIZE][4];
    int stream_size &#61; 0;
    while(scanf(&#34;%s&#34;, stream[stream_size&#43;&#43;])) {
        if(getchar() !&#61; &#39; &#39;) break;
    }

    int i &#61; 0;
    while(i &lt; stream_size) {
        char* tag &#61; stream[i&#43;&#43;];

        char* tmp1 &#61; stream[i&#43;&#43;];
        char* tmp2 &#61; stream[i&#43;&#43;];

        char tmp[5] &#61; {&#39;\0&#39;};
        strcat(tmp, tmp2);
        strcat(tmp, tmp1);

        int len;
        sscanf(tmp, &#34;%x&#34;, &amp;len);


        char res[len * 5];
        memset(res, &#39;\0&#39;, len*5);

        for(int j &#61; 0; j &lt; len; j&#43;&#43;) {
            strcat(res, stream[i&#43;&#43;]);
            if(j &lt; len - 1) {
                strcat(res, &#34; &#34;);
            }
        }

        if(strcmp(tag, target) &#61;&#61; 0) {
            puts(res);
            return 0;
        }
    }

    return 0;
}</code></pre> 
<p></p>
                </div>
        </div>
        <div id="treeSkill"></div>
        <div id="blogExtensionBox" style="width:400px;margin:auto;margin-top:12px" class="blog-extension-box"></div>
    <script>
  $(function() {
    setTimeout(function () {
      var mathcodeList = document.querySelectorAll('.htmledit_views img.mathcode');
      if (mathcodeList.length > 0) {
        for (let i = 0; i < mathcodeList.length; i++) {
          if (mathcodeList[i].naturalWidth === 0 || mathcodeList[i].naturalHeight === 0) {
            var alt = mathcodeList[i].alt;
            alt = '\\(' + alt + '\\)';
            var curSpan = $('<span class="img-codecogs"></span>');
            curSpan.text(alt);
            $(mathcodeList[i]).before(curSpan);
            $(mathcodeList[i]).remove();
          }
        }
        MathJax.Hub.Queue(["Typeset",MathJax.Hub]);
      }
    }, 1000)
  });
</script>
</div></html>